<template>
  <div>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <van-card v-for="(item, index) in goodColumns" :key="index" currency>
          <template #thumb>
            <van-field name="checkbox" style="background-color:#FAFAFA">
              <template #input>
                <van-checkbox
                  v-model="item.checkbox"
                  shape="round"
                  class="checkbox-content"
                  @click="onTest(item)"
                />
              </template>
            </van-field>
          </template>
          <template #desc>
            <div style="text-align:left;color:#cccccc">{{item.code}}</div>
          </template>
          <template #price>
            <!-- 最低售价：￥ -->
            <div style="float:left;margin-right:20px">￥{{item.invSCost}}</div>
            <van-stepper style="float:left" v-model="item.quantity" theme="round" button-size="18" />
          </template>
          <template #title>
            <div style="text-align:left">{{item.name}}</div>
          </template>
          <template #tags>
            <van-tag plain class="tag-content" type="primary">单位：{{item.unitName}}</van-tag>
            <van-tag plain class="tag-content" type="primary">单位2：{{item.unitName2}}</van-tag>
            <van-tag plain round class="tag-content" type="success">{{item.inventoryclassName}}</van-tag>
          </template>
        </van-card>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { clearArr } from "@/api/public/public";
import { Toast } from "vant";
import { queryGoods } from "@/api/order/order";
export default {
  data() {
    return {
      // 查询条件
      condition: {},
      // 存货分类ID
      idinventoryclass: "",
      // 存货数组
      goodColumns: [],
      // tab页
      activeName: "a",
      // 账套
      DsName: "",
      // 客户id
      idcustomer: "",
      // 下拉翻页
      list: [],
      page: 1,
      limit: 10,
      count: "", //总数
      loading: false,
      finished: false,
      refreshing: false,
    };
  },
  created() {
    // 从session中获取账套
    this.DsName = JSON.parse(this.$session.get("DsName"));
    // 从session中客户id
    this.idcustomer = this.$store.state.orderData.orderData.formData.idcustomer;
  },
  methods: {
    // 添加赠品
    onAddGift(item) {
      item.gift = 1;
      item.invSCost = 0;
      this.$emit("fatherMethod", item);
    },
    // 多选
    onTest(item) {},
    // 下拉刷新事件
    onLoad() {
      this.findGoods();
    },
    onRefresh() {
      // 清空列表数据
      this.finished = false;
      this.page = 1;
      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true;
      this.onLoad();
    },
    // 查询存货
    findGoods() {
      this.condition.DsName = this.DsName;
      this.condition.idcustomer = this.idcustomer;
      this.condition.limit = this.limit;
      this.condition.page = this.page++;
      // console.log("findGoods",  this.condition);
      queryGoods(this.condition, (data) => {
        let goodColumns = data.data;
          //  data.data.forEach(ele=>console.log("id",ele.id,"page limit",this.condition.page,this.limit));
        this.count = data.count;
        goodColumns.forEach((ele) => {
          this.goodColumns.push(ele);
        });
        // this.goodColumns = clearArr(this.goodColumns);

        this.goodColumns.filter((ele) => {
          ele.gift = 0;
        });
        // 加载状态结束
        this.loading = false;
        this.refreshing = false;
        // console.log("length", this.goodColumns.length);
        // console.log("count", this.count);

        // 数据全部加载完成
        if (this.goodColumns.length >= this.count) {
          this.finished = true;
          // console.log("finished", this.finished);
        }
      });
    },
    // 存货选择
    onConfirmGood(v) {
      Toast("选择成功!");
    },
  },
};
</script>

<style lang="scss" scoped>
.van-card__bottom {
  line-height: 20px;
  float: left;
  text-align: left;
}

.checkbox-content {
  margin-top: 16px;
}

.tag-content {
  float: left;
  margin: 2px;
}

.tag-content-2 {
  float: right;
  margin: 2px;
}
</style>
